import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/UI/userbottom_Menu_page.dart';
import 'package:magic_car/user/examquestion.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/colors.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class ExamResultPage extends StatefulWidget {
 // ExamResultPage({Key key, this.title}) : super(key: key);

 
  final String rid;
ExamResultPage(this.rid);
  @override
  _MyHomePageState createState() => new _MyHomePageState(this.rid);
}

class _MyHomePageState extends State<ExamResultPage> {
  List formList;
  String score = '0';
  String right = '0';
  String wrong = "0";
  String noans = "0";
  final String rid;
  _MyHomePageState(this.rid);
  bool pass = false;
  initState() {
    super.initState();
    formList = [
      // {
      //   "title": '车牌号111111',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
      // {
      //   "title": '车牌号',
      //   'sub_title': '云A00001',
      //   'sub_color': Color.fromRGBO(27, 182, 251, 1)
      // },
    ];
    loaddata();
  }

  //加载数据
  loaddata() async {
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["rid"] = this.rid;

    print(map);

    HttpHelper.post(HttpHelper.API_ExamRecordinfo, (data) async {
      if (data != null) {
        print(data);

        if (data["code"].toString() == "1") {
          // List temp = [];
          // temp.add(data["info"]);
          setState(() {
            score = data["info"]['score'].toString();
            right = data["info"]['right'].toString();
            wrong = data["info"]['wrong'].toString();
            noans = data["info"]['noanswer'].toString();
            pass = data["info"]['score'] < 90 ? false : true;
            // pass = int.parse(data["info"]['score']) < 90 ? false : true;
          });
        } else {
          Toast.show(data["msg"], context);
        }
      }
    }, params: map);
  }

  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;
    tiles.add(Padding(
      padding: new EdgeInsets.all(10),
      child: Container(
        width: double.infinity - 20,
        decoration: new BoxDecoration(
          color: Colors.white,
        ),
        child: new Column(children: <Widget>[
          Padding(
            padding: new EdgeInsets.fromLTRB(0, 30, 0, 20),
            child: RichText(
                text: TextSpan(
                    text: score,
                    style: new TextStyle(fontSize: 40, color: Colors.red),
                    children: [
                  TextSpan(
                    text: ' 分',
                    style: TextStyle(fontSize: 14, color: Colors.blue),
                  ),
                ])),
          ),
          RichText(
              text: TextSpan(
                  text: '正确：',
                  style: new TextStyle(fontSize: 16, color: Colors.black),
                  children: [
                TextSpan(
                  text: right,
                  style: TextStyle(fontSize: 16, color: Colors.red),
                ),
                TextSpan(
                  text: ' 题',
                  style: TextStyle(fontSize: 16, color: Colors.black),
                ),
              ])),
          Padding(
              padding: new EdgeInsets.fromLTRB(0, 10, 0, 10),
              child: GestureDetector(
                  onTap: () {
                    //查看错题
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (_) {
                      return new ExamQuesPage(this.rid, "1");
                    }));
                  },
                  child: RichText(
                      text: TextSpan(
                          text: '错误：',
                          style:
                              new TextStyle(fontSize: 16, color: Colors.black),
                          children: [
                        TextSpan(
                          text: wrong,
                          style: TextStyle(fontSize: 16, color: Colors.red),
                        ),
                        TextSpan(
                          text: ' 题',
                          style: TextStyle(fontSize: 16, color: Colors.black),
                        ),
                      ])))),
          GestureDetector(
            onTap: () {
              //查看未答
               Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (_) {
                      return new ExamQuesPage(this.rid, "2");
                    }));
            },
            child: RichText(
                text: TextSpan(
                    text: '未答：',
                    style: new TextStyle(fontSize: 16, color: Colors.black),
                    children: [
                  TextSpan(
                    text: noans,
                    style: TextStyle(fontSize: 16, color: Colors.red),
                  ),
                  TextSpan(
                    text: ' 题',
                    style: TextStyle(fontSize: 16, color: Colors.black),
                  ),
                ])),
          ),
          Padding(
              padding: new EdgeInsets.fromLTRB(0, 10, 0, 20),
              child: new Text(
                pass == true ? '此次考试合格，请等待线下测试' : '此次考试不合格',
                style: new TextStyle(fontSize: 16),
              )),
          pass == false
              ? Padding(
                  padding: new EdgeInsets.fromLTRB(0, 10, 0, 20),
                  child: Container(
                    padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                    height: 50,
                    decoration: new BoxDecoration(
                        // color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        border: new Border.all(width: 0, color: Colors.blue)),
                    child: SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: new FlatButton(
                          child: Text(
                            '继续考试',
                            style: TextStyle(
                                color: Color.fromRGBO(255, 255, 255, 1),
                                fontSize: 16),
                          ),
                          color: Colors.blue,
                          textColor: Colors.white,
                          onPressed: () async {
                            Navigator.pushNamed(context, "exam");
                          },
                        )),
                  ))
              : Padding(
                  padding: new EdgeInsets.fromLTRB(0, 10, 0, 20),
                  child: Container(
                    padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                    height: 50,
                    decoration: new BoxDecoration(
                        // color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        border: new Border.all(width: 0, color: Colors.blue)),
                    child: SizedBox(
                        width: double.infinity,
                        height: 50,
                        child: new FlatButton(
                          child: Text(
                            '返回首页',
                            style: TextStyle(
                                color: Color.fromRGBO(255, 255, 255, 1),
                                fontSize: 16),
                          ),
                          color: Colors.blue,
                          textColor: Colors.white,
                          onPressed: () async {
                            backhome();
                          },
                        )),
                  ))
        ]),
      ),
    ));

    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  backhome() {
    final PageRouteBuilder _homeRoute = new PageRouteBuilder(
      pageBuilder: (BuildContext context, _, __) {
        return UBotomeMenumPage();
      },
    );
    Navigator.pushAndRemoveUntil(
        context, _homeRoute, (Route<dynamic> r) => false);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            backhome();
          },
          child: Icon(
            Icons.home,
            color: Colors.white,
          ),
          backgroundColor: Colours.app_main,
        ),
        appBar: new AppBar(
          title: new Text('考试结果'),
          backgroundColor: Colors.white,
          elevation: 0,
        ),
        body: new Center(
          child: Container(
              decoration: new BoxDecoration(
                color: Color.fromRGBO(238, 238, 238, 1),
              ),
              child: new ListView(
                children: <Widget>[buildcontent()],
              )),
        ));
  }
}
